package com.codingfuture.glacierdataapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codingfuture.glaciercommon.util.PageParam;
import com.codingfuture.glacierdataapi.dto.AlarmProcessRecordDTO;
import com.codingfuture.glacierdataapi.entity.AlarmRecord;
import com.codingfuture.glacierdataapi.mapper.*;
import com.codingfuture.glacierdataapi.vo.AlarmModeVo;
import com.codingfuture.glaciercommon.entity.AlarmProcessMode;
import com.codingfuture.glaciercommon.entity.AlarmProcessRecord;
import com.codingfuture.glaciercommon.entity.Warehouse;
import com.codingfuture.glacierdataapi.service.DataApiService;
import com.codingfuture.glaciercommon.entity.MeterRecord;
import com.codingfuture.glaciercommon.util.EntityConverter;
import com.codingfuture.glacierdataapi.vo.AlarmRecordVO;
import com.codingfuture.glacierdataapi.vo.WarehouseAndMeterVO;
import com.codingfuture.glacierdataapi.vo.WarehouseDataVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import java.time.format.DateTimeFormatter;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Ji
 * @DateTime: 2025-04-09 17:10
 * @Description:
 **/
@Slf4j
@Service
public class DataApiServiceImpl implements DataApiService {

//    @Autowired
//    private MeterRecordMapper meterRecordMapper;
    @Autowired
    private DataApiMapper dataApiMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private AlarmProcessModeMapper alarmProcessModeMapper;
    @Autowired
    private AlarmProcessRecordMapper alarmProcessRecordMapper;
    @Autowired
    private AlarmRecordMapper alarmRecordMapper;

    @Cacheable(value = "warehouseDataVO")
    @Override
    public List<WarehouseDataVO> getWarehouse(Long ids, Long companyId) {
        if (ids > 2) {
            List<WarehouseDataVO> warehouse1 = warehouseMapper.getWarehouse1(companyId);
            log.debug("warehouse1:{}", warehouse1);
            return warehouse1;
        }
        List<WarehouseDataVO> warehouse2 = warehouseMapper.getWarehouse2(ids, companyId);
        log.debug("warehouse2:{}", warehouse2);
        return warehouse2;
    }

//    @Cacheable(value = "meter_recordVOS")
    @Override
    public List<WarehouseAndMeterVO> getMeterRecord(PageParam pageParam,
                                                    Long companyId, Long statusesId) {
        Integer pageNum = pageParam.getPageNum();
        Integer pageSize = pageParam.getPageSize()*(pageParam.getPageNum()-1);
        if (statusesId > 2) {
            return dataApiMapper.selectByPage1(pageNum,pageSize, companyId);
        }
        return dataApiMapper.selectByPage2(pageNum,pageSize, companyId, statusesId);
    }

    @Cacheable(value = "alarm_process_mode")
    @Override
    public List<AlarmModeVo> getMode() {
        List<AlarmProcessMode> alarmProcessModes = alarmProcessModeMapper.selectList(null);
        List<AlarmModeVo> alarmModeVos = new ArrayList<>();
        for (AlarmProcessMode alarmProcessMode : alarmProcessModes) {
            AlarmModeVo alarmModeVo = EntityConverter.entity2DTO(alarmProcessMode, AlarmModeVo.class);
            alarmModeVos.add(alarmModeVo);
        }
        return alarmModeVos;
    }

    @CacheEvict(value = "alarm_process_record", allEntries = true)
    @Override
    public boolean add(AlarmProcessRecordDTO alarmProcessRecordDTO) {
        AlarmProcessRecord alarmProcessRecord = alarmProcessRecordDTO.convert2Entity();
        alarmProcessRecord.setCreateTime(LocalDateTime.now());
        return alarmProcessRecordMapper.insert(alarmProcessRecord) > 0;
    }

    @Cacheable(value = "alarm_record")
    @Override
    public Page<AlarmRecord> getPage(Page<AlarmRecord> page, String warehouseSn,
                                     LocalDateTime startTime, LocalDateTime endTime) {
        QueryWrapper<AlarmRecord> alarmRecordQueryWrapper = new QueryWrapper<>();
        alarmRecordQueryWrapper.eq(warehouseSn == null, "warehouse_sn", warehouseSn)
                .gt(startTime != null, "start_time", startTime)
                .lt(endTime != null, "end_time", endTime);
        return alarmRecordMapper.selectPage(page, alarmRecordQueryWrapper);
    }

    @Override
    public Long count(String warehouseSn, LocalDateTime startTime, LocalDateTime endTime) {
        QueryWrapper<AlarmRecord> alarmRecordQueryWrapper = new QueryWrapper<>();
        alarmRecordQueryWrapper.eq(warehouseSn == null, "warehouse_sn", warehouseSn)
                .gt(startTime != null, "start_time", startTime)
                .lt(endTime != null, "end_time", endTime);
        return alarmRecordMapper.selectCount(alarmRecordQueryWrapper);
    }


}
